// jest测试文件
// const JsEventsEmitter = require('../dist/js-events-emitter')

import JsEventsEmitter from '../dist/js-events-emitter.esm.js'

let event = new JsEventsEmitter()

let _this = new Object()

let event1 = 'test1'
let event2 = 'test2'
let event3 = 'test3'

let obj = {
  a: 555
}

let num = 555

let str = '555'

let bool = false

let befor = 66

// test on
let fn = function (p0, p1, p2, p3, p4, p5) {
  expect(this).toBe(_this)
  expect(p0).toBe(befor)
  expect(p1).toBe(num)
  expect(p2).toBe(str)
  expect(p3).toBe(fn)
  expect(p4).toBe(bool)
  expect(p5).toBe(obj)
}

let fn1 = function () {}

event.on(event1, _this, fn, [befor])

event.on(event1, _this, fn1)

event.on(event2, _this, function () {
  console.log(8888)
})

// once test
let fn2 = function (count) {
  expect(count).toBe(0)
}
event.once(event3, _this, fn2)

// event
test('test on、emit', () => {
  event.emit(event1, [num, str, fn, bool, obj])
})

// event
test('test once', () => {
  let count = 0
  event.emit(event3, [count])
  count++
  event.emit(event3, [count])
})

test('test getEventNames getListener', () => {
  let eventNames = event.getEventNames()
  expect(eventNames.length).toBe(3)
  let len1 = event.getListener(event1).length
  expect(len1).toBe(2)
})

test('test off offAll', () => {
  event.off(event1, fn)
  let len = event.getEventNames().length
  expect(len).toBe(3)

  let listener = event.getListener(event1)
  expect(listener.length).toBe(1)

  event.off(event1, fn1)
  len = event.getEventNames().length
  expect(len).toBe(3)
  listener = event.getListener(event1)
  expect(listener && listener.length).toBe(0)

  event.offAll()
  len = event.getEventNames().length
  expect(len).toBe(0)
  listener = event.getListener(event1)
  expect(listener).toBeUndefined()
})
